package com.rustic.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.enums.RoleEnum;
import cn.xy.base.server.po.SysAdmin;
import cn.xy.base.server.po.SysRole;
import cn.xy.base.server.service.SysAdminService;
import cn.xy.base.server.service.SysRoleService;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.exception.JzRuntimeException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rustic.server.dao.RusticAdminDao;
import com.rustic.server.po.RusticAdmin;
import com.rustic.server.query.RusticAdminAdminListQuery;
import com.rustic.server.service.RusticAdminService;
import com.rustic.server.vo.list.RusticAdminAdminListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * RusticAdmin对应的服务类实现
 *
 * @author auto
 */

@Service
public class RusticAdminServiceImpl extends ServiceImpl<RusticAdminDao, RusticAdmin> implements RusticAdminService {

    @Autowired
    private SysAdminService sysAdminService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public List<RusticAdmin> listByIdIn(List<Integer> idList) {
        if (CollUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        return this.listByIds(idList);
    }

    @Override
    public Map<Integer, RusticAdmin> mapByIdIn(List<Integer> idList) {
        List<RusticAdmin> rusticAdminList = this.listByIdIn(idList);
        return rusticAdminList.stream().collect(Collectors.toMap(RusticAdmin::getId, b -> b));
    }

    @Override
    public LambdaQueryWrapper<RusticAdmin> toAdminListWrapper(RusticAdminAdminListQuery query) {
        LambdaQueryWrapper<RusticAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RusticAdmin::getRusticId, query.getRusticId());
        queryWrapper.eq(query.getRoleId() > 0, RusticAdmin::getRoleId, query.getRoleId());
        queryWrapper.gt(query.getStartCreateTime() != null, RusticAdmin::getCreateTime, query.getStartCreateTime());
        queryWrapper.lt(query.getEndCreateTime() != null, RusticAdmin::getCreateTime, query.getEndCreateTime());
        return queryWrapper;
    }

    @Override
    public List<RusticAdminAdminListVO> toAdminListVo(List<RusticAdmin> rusticAdminList, List<SysAdmin> sysAdminList ) {
        Map<Integer, RusticAdmin> rusticAdminMap = rusticAdminList.stream().collect(Collectors.toMap(RusticAdmin::getAdminId, b -> b));
        Map<Integer, SysRole> roleMap = sysRoleService.getMap();
        return sysAdminList.stream().map(item -> {
            RusticAdminAdminListVO rusticAdminVO = new RusticAdminAdminListVO(item);
            RusticAdmin rusticAdmin = rusticAdminMap.get(item.getId());
            if (rusticAdmin == null) {
                rusticAdminVO.setRoleId(0);
                rusticAdminVO.setCreateTime(null);
                return rusticAdminVO;
            }
            SysRole role = roleMap.get(rusticAdmin.getRoleId());
            if (role != null) {
                rusticAdminVO.setRoleName(role.getRoleName());
            }
            rusticAdminVO.setRusticId(rusticAdmin.getRusticId());
            rusticAdminVO.setRoleId(rusticAdmin.getRoleId());
            rusticAdminVO.setCreateTime(rusticAdmin.getCreateTime());
            return rusticAdminVO;
        }).collect(Collectors.toList());
    }

    @Override
    public Page<RusticAdminAdminListVO> getPageList(RusticAdminAdminListQuery query) {
        // 查询关联表
        LambdaQueryWrapper<RusticAdmin> queryWrapper = this.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return new Page<>();
        }
        Page<RusticAdmin> pageData = this.page(query.toPage(), queryWrapper);
        List<RusticAdmin> rusticAdminList = pageData.getRecords();
        if (CollUtil.isEmpty(rusticAdminList)) {
            return new Page<>();
        }
        // 获取管理员id查询管理员基本信息
        List<Integer> adminIdList = rusticAdminList.stream().map(RusticAdmin::getAdminId).collect(Collectors.toList());
        List<SysAdmin> sysAdmins = sysAdminService.listByIdsAndUsername(adminIdList, query.getUsername());
        List<RusticAdminAdminListVO> adminAdminListVOS = this.toAdminListVo(rusticAdminList, sysAdmins);
        return PageUtil.toPage(pageData, adminAdminListVOS);
    }

    @Override
    public Map<Integer, Integer> getRusticAndRoleByAdminId(int adminId) {
        List<RusticAdmin> list = this.list(new LambdaQueryWrapper<RusticAdmin>()
                .eq(RusticAdmin::getAdminId, adminId));
        if (CollUtil.isEmpty(list)) {
            return new HashMap<>();
        }
        return list.stream().collect(Collectors.toMap(RusticAdmin::getRusticId, RusticAdmin::getRoleId));
    }

    @Override
    public List<Integer> getRusticIdByAdminId(int adminId) {
        List<RusticAdmin> list = this.list(new LambdaQueryWrapper<RusticAdmin>()
                .eq(RusticAdmin::getAdminId, adminId));
        return list.stream().map(RusticAdmin::getRusticId).collect(Collectors.toList());
    }

    @Override
    public RusticAdmin getByAdminIdAndRusticId(int adminId, int rusticId) {
        return this.getOne(new LambdaQueryWrapper<RusticAdmin>()
                .eq(RusticAdmin::getAdminId, adminId)
                .eq(RusticAdmin::getRusticId, rusticId));
    }

    @Override
    public void saveLinkBatch(int adminId, List<Integer> rusticIdList, int roleId) {
        List<RusticAdmin> rusticAdminList = rusticIdList.stream().map(item -> {
            RusticAdmin rusticAdmin = new RusticAdmin();
            rusticAdmin.setAdminId(adminId);
            rusticAdmin.setRusticId(item);
            rusticAdmin.setRoleId(roleId);
            return rusticAdmin;
        }).collect(Collectors.toList());
        this.saveBatch(rusticAdminList);
    }

    @Override
    public void updateLink(int adminId, int rusticId, int roleId) {
        RusticAdmin rusticAdmin = this.getByAdminIdAndRusticId(adminId, rusticId);
        if (rusticAdmin == null) {
            rusticAdmin = new RusticAdmin();
            rusticAdmin.setAdminId(adminId);
            rusticAdmin.setRusticId(rusticId);
            rusticAdmin.setRoleId(roleId);
            this.save(rusticAdmin);
            return;
        }
        // 更新
        rusticAdmin.setRoleId(roleId);
        this.updateById(rusticAdmin);
    }

    @Override
    public int getAdminRoleMax(int adminId) {
        List<RusticAdmin> list = this.list(new LambdaQueryWrapper<RusticAdmin>()
                .eq(RusticAdmin::getAdminId, adminId));
        List<Integer> rusticIdList = list.stream().map(RusticAdmin::getRoleId).collect(Collectors.toList());
        if (CollUtil.isEmpty(rusticIdList)) {
            return 0;
        }
        return CollUtil.min(rusticIdList);
    }

    @Override
    public void deleteLink(int adminId, int rusticId, int operateAdminId) {
        // 需要修改的管理员信息
        SysAdmin admin = sysAdminService.getById(adminId);
        // 操作人的信息
        SysAdmin operateAdmin = sysAdminService.getById(operateAdminId);
        // 运营管理员不能操作
        if (operateAdmin.getRoleId() == RoleEnum.OPERATION_ADMIN.getId()) {
            throw new JzRuntimeException("您的权限不足");
        }
        // 乡村管理员不能修改超级管理员
        if (RoleEnum.isSuperAdmin(admin.getRoleId()) && !RoleEnum.isSuperAdmin(operateAdmin.getRoleId())) {
            throw new JzRuntimeException("您没有权限修改超级管理员账号");
        }
        this.remove(new LambdaUpdateWrapper<RusticAdmin>()
                .eq(RusticAdmin::getAdminId, adminId)
                .eq(RusticAdmin::getRusticId, rusticId));
    }

}
